package com.egglazy.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.egglazy.Entity.Category;
import com.egglazy.Service.CategoryService;
import com.egglazy.Utils.Result;
import com.egglazy.Utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

@Tag(name = "分类模块")
@RestController
@RequestMapping("/category")
public class CategoryController {

    private final CategoryService categoryService;
    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @Operation(summary = "添加分类信息")
    @PostMapping("/addInfo")
    public Result<String> addInfo(@Schema(description = "1.卡片类型,2.商城分类，3.授权状态，4.所属卡池，5.获取方式") @RequestParam("type") int type,
                                  @Schema(description = "类型名称") @RequestParam("typeValue") String typeValue) {
        Category category = new Category();
        category.setType(type);
        category.setTypeValue(typeValue);
        return categoryService.save(category) ? ResultUtil.success("添加成功") : ResultUtil.fail("添加失败！");
    }


    @Operation(summary = "根据id删除分类信息")
    @GetMapping("/deleteById")
    public Result<String> deleteById(@RequestParam("id") String id) {
        return categoryService.removeById(id) ? ResultUtil.success("删除成功") : ResultUtil.fail("删除失败！");
    }


    @Operation(summary = "根据id更新分类信息")
    @PostMapping("/updateById/{id}")
    public Result<String> updateById(@Schema(description = "1.卡片类型,2.商城分类，3.授权状态，4.所属卡池，5.获取方式") @RequestParam("type") int type,
                                     @Schema(description = "类型名称") @RequestParam("typeValue") String typeValue,
                                     @PathVariable("id") String id){
        Category category = new Category();
        category.setType(type);
        category.setTypeValue(typeValue);
        category.setId(id);
        return categoryService.updateById(category) ? ResultUtil.success("更新成功") : ResultUtil.fail("更新失败！");
    }

    @Operation(summary = "根据id查询分类信息")
    @GetMapping("/getById")
    public Result<Category> getById(@RequestParam("id") String id) {
        return ResultUtil.success(categoryService.getById(id));
    }


    @Operation(summary = "获取分类列表")
    @GetMapping("/getList")
    public Result<Page<Category>> getList(@RequestParam("pageIndex") int pageIndex,@RequestParam("pageSize") int pageSize) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        Page<Category> page = categoryService.page(new Page<>(pageIndex, pageSize), queryWrapper);
        return ResultUtil.success(page);
    }



}
